var api = require('../../config/api.js');
var util = require('../../utils/utils.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    derviceInfo: {},
    derviceDataList:[],
    monitorTime: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this

    var derviceInfo = JSON.parse(options.derviceInfo)
    that.setData({
      derviceInfo:derviceInfo
    })

    that.doHttpGetMonitorDateList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 获取监测数据
   */
  doHttpGetMonitorDateList: function () {
    var that = this

    var date = util.formatTime(new Date())
    var ymd = date.substring(0,10)

    let startTime = ymd + 'T00:00:00.000+08:00'
    let endTime = ymd + 'T23:59:59.000+08:00'

    wx.request({
      url: api.getMonitorDateList,
      method: 'POST',
      data: {
        deviceID: that.data.derviceInfo.deviceID,
        startTime: startTime,
        endTime: endTime,
        pageNum: 1,
        pageSize: 1
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            derviceDataList: res.data.data.historyMonitor.historyMonitorDataList,
            monitorTime: res.data.data.historyMonitor.monitorTime
          });
        }
      },
    })
  },

  /**
   * 点击传感器Item
   */
  onClickItem: function (e) {
    wx.request({
      url: api.querySensorToSensorID,
      method: 'POST',
      data: {
        sensorID: e.currentTarget.dataset.idtype.resourceID,
        pageNum: 1,
        pageSize: 1
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showModal({
            title: '传感器监测溯源',
            content: res.data.data.sensorName,
            cancelText: '取消',
            confirmText: '复制',
            success: function (sres) {
              if (sres.confirm) {
                wx.setClipboardData({
                  data: res.data.data.sensorName,
                  success: function (copyRes) {
                    wx.getClipboardData({
                      success: function (copyRes) {
                        console.log(copyRes.data) 
                      }
                    })
                  }
                })
              } else {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
    })
  }
})